//
//  SinglePicker.m
//  YaoHeAPP
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 mac. All rights reserved.
//
//类别、有效期、发票、可以调用当前的视图

#import "SinglePicker.h"
#define App_Frame_Height        [UIScreen mainScreen].bounds.size.height
#define App_Frame_Width         [[UIScreen mainScreen] bounds].size.width
#define Line_Low_Color   [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1]
//红色
#define  RedColor  [UIColor colorWithRed:216.0/255.0 green:12.0/255.0 blue:24.0/255.0 alpha:1]

@implementation SinglePicker

+(void)showPickerView:(UIView *)selfView withdataArray:(NSArray *)dataArray withTitle:(NSString *)titleString  singleDoble:(BOOL)yesORno  andSelectBlock:(void (^)(NSString *))selectRow 
{
    //创建pickerView
    SinglePicker*pickerView=[[SinglePicker alloc]initWithFrame:selfView.bounds];
    pickerView.pickData=dataArray;
    pickerView.SelectBlock=selectRow;
    pickerView.titleStr=titleString;
    [pickerView setSubviews];
    pickerView._YESORNO=yesORno;
    [selfView addSubview:pickerView];
    
}
//初始化大小
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfAction)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self performSelector:@selector(showSelfAction) withObject:nil afterDelay:0];
    return self;
}

//设置，要弹出的视图上显示的东西
-(void)setSubviews
{
    str1=[NSString string];
    str2=[NSString string];
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, App_Frame_Width, 260)];
    _bgView.backgroundColor = Line_Low_Color;
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = .3;
    _bgView.layer.shadowOffset = CGSizeMake(0, -1);
    [self addSubview:_bgView];

    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake((App_Frame_Width - 140)/2, 0, 140, 40)];
    titleL.backgroundColor = [UIColor clearColor];
    titleL.font = [UIFont systemFontOfSize:17];
    titleL.textColor = [UIColor whiteColor];
    titleL.text = self.titleStr;
  
    [_bgView addSubview:titleL];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.backgroundColor = [UIColor clearColor];
    confirmBtn.layer.cornerRadius = 4;
    confirmBtn.layer.borderWidth = 0.5;
    confirmBtn.layer.borderColor = RedColor.CGColor;
    confirmBtn.frame = CGRectMake(App_Frame_Width - 105, 7, 90, 32);
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:RedColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:confirmBtn];

    //初始化pickerView，设置数据来源
    //NSMutableArray*array1=[[NSMutableArray alloc]init];
    _hoursArray=[[NSMutableArray alloc]init];
    _minutesArray=[[NSMutableArray alloc]init];
    for (NSInteger i=0; i<24 ;i++)
    {
        NSString*string=[NSString stringWithFormat:@"%ld小时",(long)i];
        [_hoursArray addObject:string];
    }
    
    // NSMutableArray*array2=[[NSMutableArray alloc]init];
    for (NSInteger i=0; i<60; i++)
    {
        if (i%5==0)
        {
            NSString*string=[NSString stringWithFormat:@"%ld分钟",(long)i];
            [_minutesArray addObject:string];
        }
    }
    _dataArray=self.pickData;
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, App_Frame_Width, 216)];
    _pickerView.backgroundColor = [UIColor whiteColor];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_bgView addSubview:_pickerView];
}
//点击确定按钮执行的方法
- (void)confirmBtnAction
{
    if (self._YESORNO==NO)
    {
        if (str1.length==0)
        {
            str1=_hoursArray[0];
        }
        if (str2.length==0)
        {
            str2=_minutesArray[0];
        }
        NSString*string=[NSString stringWithFormat:@"%@-%@",str1,str2];
        _stringRow=string;
    }
    else if(_stringRow==nil)
    {
        _stringRow=_dataArray[0];
    }
    self.SelectBlock(_stringRow);
    [self removeSelfAction];
}
//展示出选择器
- (void)showSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0, -260);
    } completion:^(BOOL finished) {
        
    }];
}
//移除选择器
- (void)removeSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform  = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

#pragma mark----------------选择器的代理方法
//返回每行的高度
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
{
    return 40;
}
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self._YESORNO==NO)
    {
        return 2;
    }
    return 1;
}
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self._YESORNO==NO)
    {
        if (component==0)
        {
            return _hoursArray.count;
        }
        return _minutesArray.count;
    }
    return _dataArray.count;
}

//自己定义选择器上现实的东西
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    if (self._YESORNO==YES)
    {
        //创建一个View，在view上放置一个label，这样可以更改字体，颜色，等属性，view上还可以放置其他视图
        UIView *myView = [[UIView alloc]init];
        myView.frame =CGRectMake(0.0f, 0.0f, pickerView.frame.size.width, 40);
        UILabel *txtlabel = [[UILabel alloc] init];
        txtlabel.textAlignment = NSTextAlignmentCenter;
        txtlabel.frame = myView.frame;
        txtlabel.text=_dataArray[row];
        [myView addSubview:txtlabel];
        return myView;
    }
   
    else
    {
        if (component==0)
        {
            UIView *myView = [[UIView alloc]init];
            myView.frame =CGRectMake(0.0f, 0.0f, pickerView.frame.size.width, 40);
            UILabel *txtlabel = [[UILabel alloc] init];
            txtlabel.textAlignment = NSTextAlignmentCenter;
            txtlabel.frame = myView.frame;
            txtlabel.text=_hoursArray[row];
            [myView addSubview:txtlabel];
            return myView;
        }
        UIView *myViewt=[[UIView alloc]init];
        myViewt.frame =CGRectMake(0, 0.0f, pickerView.frame.size.width, 40);
        UILabel *txtlabel2 = [[UILabel alloc] init];
        txtlabel2.textAlignment = NSTextAlignmentCenter;
        txtlabel2.frame = myViewt.frame;
        txtlabel2.text=_minutesArray[row];
        [myViewt addSubview:txtlabel2];
        return myViewt;
    }
}

//获取当前选中的是哪一行（用block回调出去）
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
{
    if (self._YESORNO==YES)
    {
        _stringRow=_dataArray[row];
    }
    else
    {
        if (component==0)
        {
            str1=_hoursArray[row];
        }
        if (component==1)
        {
            str2=_minutesArray[row];
        }
    }
}

#pragma tap  delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_bgView.frame, point)) {
        return NO;
    }
    
    return YES;
}


@end
